const minioClient = require('../services/minioClient');
const path = require('path');

// 上传图片到 MinIO
exports.uploadImage = async (req, res) => {
  try {
    const file = req.file;
    const userId = req.user.id;

    if (!file) {
      return res.status(400).json({ error: 'No file uploaded' });
    }

    // 设置文件名和内容类型
    const fileName = `${userId}_${Date.now()}_${path.basename(file.originalname)}`;
    const contentType = file.mimetype;

    // 上传到 MinIO
    await minioClient.putObject(process.env.MINIO_BUCKET_NAME, fileName, file.buffer, {
      'Content-Type': contentType,
    });

    // 返回文件 URL
    const fileUrl = `http://${process.env.MINIO_ENDPOINT}:${process.env.MINIO_PORT}/${process.env.MINIO_BUCKET_NAME}/${fileName}`;
    res.status(200).json({ url: fileUrl });
  } catch (error) {
    console.error('Error uploading image:', error);
    res.status(500).json({ error: 'Internal Server Error' });
  }
};
